# -*- coding: utf-8 -*
from config import *
import requests,time
import execjs,re,os,json
from urllib.parse import urlencode
from Helper import Helper
from Helper import r
from threading import Thread
from pprint import pprint
from taobao import Taobao

list = []

class Shop( object ):

    def __init__(self):

        # 判断redis长度
        if Helper.lenRedis('tb_task') <= 0:

            t = Taobao()

            t.run()

        print('*-------------------开始执行爬虫程序---------------------*')

        # 创建excel表头信息
        # if not os.path.exists(save_file):
        #
        #     Helper.writeRes( con )

    # 请求详情页面
    def requestIndex(self,data):

        # 重置cookie
        headers['Cookie'] = data['cookies']
        args = self.genSign( data )
        # 合成请求参数
        params['t'] = args['stamp']
        params['sign'] = args['sign']
        params['data'] = args['sellerId']

        #路径生成
        prefix = urlencode(params).replace('+', '')

        try:

            #发起详情请求
            resp = requests.get( url = domain['impress_url'] + prefix ,headers = headers)

            if resp.status_code == 200:

                #处理json数据

                self.parseJson( data,resp.json())

        except Exception as e:

            print( e )


    # 生成签名
    def genSign(self,data):

        # 重新赋值

        sellerId = '{0}'.format({"sellerId": int(data['uid']), "shopId": int(data['shopId'])}).replace(' ', '')

        pattern = re.compile(r'_m_h5_tk=(.*?)_')

        root_path = os.getcwd()

        with open('{0}\\seller.js'.format(root_path), encoding='utf-8') as f:

            js = f.read()

        cxt = execjs.compile(js)

        stamp = int(time.time() * 1000)

        token = re.search(pattern, headers['Cookie']).group(1)

        data = {

            'token': token,
            'stamp': str(stamp),
            'keys': '12574478',
            'sellerId': sellerId
        }

        args = ''

        for k in data.values():
            args = args + '&' + k

        sign = cxt.call('h', args[1:])

        data['sign'] = sign

        return data

    def parseJson(self,params,items):
        # 判断异常清空cookie
        if 'SUCCESS::调用成功' not in items['ret']:
            # {'api': 'mtop.taobao.shop.impression.intro.get', 'v': '1.0', 'ret': ['FAIL_SYS_SERVICE_INNER_FAULT::服务内部故障'], 'data': {}}
            if '服务内部故障' not in json.dumps(items, ensure_ascii=False):
                # 回执任务到redis队列
                params.pop('cookies')

                # Helper.saveRedis( params )
                callback = json.dumps(params, ensure_ascii=False)
                r.rpush('tb_task', callback)

                print('账号被封禁,重新写入redis成功:', params['nick'])

                return None

            else:

                print('服务内部错误,不做任何处理:', params['nick'])


        else:

            pprint(items)

            data = items['data']['result']

            # 存储到mongoDB方法

            res = {

                'title':params['title'],
                'nick':data['nick'],
                'phone':data['phone'],
                'uid':data['sellerId'],
                'city':data['city'],
                'date':time.strftime('%Y-%m-%d', time.localtime(time.time())).replace('-', '')

            }

            Helper.saveMongo( res )

    def main(self):

        while True:

            # 获取redis任务
            res = Helper.getRedis()

            if res == None:

                # print('*---------数据采集完成,输入任意键打开采集结果-------*')
                break



            data = {
                'title':res['title'],
                'nick':res['nick'],
                'uid':res['uid'],
                'city':res['city'],
                'shopId':res['shopId'],
                'cookies':open('./cookies.txt',mode='r',encoding='utf-8').read(),
            }
            self.requestIndex( data )


if __name__ == '__main__':

    s = Shop()

    threads = []

    for i in range( max_threads ):

        t = Thread( target=s.main,args=())

        t.start()
